<template>
  <BoTable
    :columns="columnsAll"
    :request="_getDevicePushAllList"
    ref="tableRef"
    :row-selection="rowSelection"
    :show-index="true"
    row-key="versionId"
    height="auto"
  ></BoTable>

  <!-- 查看设备推送记录 -->
  <Modal
    v-model:open="open"
    title="推送列表"
    width="80%"
    destroyOnClose
    :maskClosable="false"
    :keyboard="false"
    centered
  >
    <BoTable
      :columns="columns"
      :request="_getDeviceLogList"
      ref="tableDeviceRef"
      row-key="deviceId"
      height="auto"
      show-index
      hideSearch
    >
    </BoTable>
    <template #footer></template>
  </Modal>
  <!-- 重推弹窗 -->
  <SseModal />
</template>

<script setup lang="tsx">
import { computed, ref } from 'vue';
import { getDeviceModelSelect, getDeviceProjectSelect } from '@/api';
import { deviceEnum } from '@/constants';
import { useDicts } from '@boeyo/portal';
import { listToEnum, listToOption } from '@boeyo/shared';
import { BoTable, BoTableProps } from '@boeyo/ui';
import { Modal, message } from 'ant-design-vue';
import { useSseModal } from '@/hooks/useSseModal';
import { getDeviceLogList, getDevicePushAllList } from '../../api';

const props = defineProps<{
  versionId: number;
}>();

// 总记录
const tableRef = ref();
const selectKeys = ref<number[]>([]);
const rowSelection = computed<BoTableProps['rowSelection']>(() => {
  return {
    selectedRowKeys: selectKeys.value,
    onChange(values, all) {
      selectKeys.value = values as any;
    },
  };
});
// 设备推送记录
const tableDeviceRef = ref();

const dictArr = ['device_type', 'push_status'];
const { deviceTypeEnum, pushStatusEnum } = useDicts(dictArr);

// 获取表单下拉框数据
const projectOptions = ref<any[]>([]);
const projectEnum = ref<Record<string, any>>({});
const modelOptions = ref<any[]>([]);
const modelEnum = ref<Record<string, any>>({});
const init = () => {
  getDeviceProjectSelect().then(({ data }) => {
    projectOptions.value = listToOption(data, 'name', 'projectId');
    projectEnum.value = listToEnum(data, 'name', 'projectId');
  });
  getDeviceModelSelect(deviceEnum.SEISMOGRAPH).then(({ data }) => {
    modelOptions.value = listToOption(data, 'model', 'modelId');
    modelEnum.value = listToEnum(data, 'model', 'modelId');
  });
};
init();

const columnsAll = computed<BoTableProps['columns']>(() => [
  {
    title: '推送时间',
    dataIndex: 'upgradeTime',
    hideInSearch: true,
  },
  {
    title: '推送总数',
    dataIndex: 'totalCount',
    hideInSearch: true,
  },
  {
    title: '推送成功数',
    dataIndex: 'successCount',
    hideInSearch: true,
  },
  {
    title: '推送失败数',
    dataIndex: 'failCount',
    hideInSearch: true,
  },
  {
    title: '推送人',
    dataIndex: 'pushBy',
    hideInSearch: true,
  },
  {
    title: '操作',
    dataIndex: 'operation',
    hideInSearch: true,
    fixed: 'right',
    actions: [
      {
        text: '查看记录',
        onClick(record) {
          onPushRecord(record);
        },
      },
    ],
  },
]);
const columns = computed<BoTableProps['columns']>(() => [
  {
    title: '推送时间',
    dataIndex: 'upgradeTime',
    hideInSearch: true,
  },
  {
    title: '设备序列号',
    dataIndex: 'deviceCode',
    hideInSearch: true,
  },
  {
    title: '设备类型',
    dataIndex: 'deviceType',
    valueEnum: deviceTypeEnum.value,
    valueType: 'select',
  },
  {
    title: '设备型号',
    dataIndex: 'model',
    hideInSearch: true,
  },
  {
    title: '推送状态',
    dataIndex: 'upgradeStatus',
    valueType: 'status',
    valueEnum: pushStatusEnum.value,
  },
  {
    title: '操作',
    dataIndex: 'operation',
    hideInSearch: true,
    actions({ record }) {
      return [
        {
          text: '重推',
          onClick: () => {
            onPush(record);
          },
          disabled(record) {
            return record.upgradeStatus == 0;
          },
        },
      ];
    },
  },
]);

const _getDevicePushAllList = async (params) => {
  params.versionId = props.versionId;
  return await getDevicePushAllList(params);
};

// 查看设备推送记录
const open = ref(false);
const versionPushTotalId = ref(0);
const onPushRecord = (record) => {
  open.value = true;
  versionPushTotalId.value = record.versionPushTotalId;
};

// 设备推送记录
const _getDeviceLogList = async (params) => {
  params.versionPushTotalId = versionPushTotalId.value;
  return await getDeviceLogList(params);
};

const { SseModal, open: openSse } = useSseModal();
const onPush = (record) => {
  Modal.confirm({
    title: '系统提示',
    content: `确定重推当前选中项数据吗?`,
    onOk: async () => {
      // SSE方式重推
      if (
        record.deviceId == '' ||
        props.versionId == null ||
        record.versionPushId == ''
      ) {
        message.info('请选择设备');
        throw new Error('请选择设备');
      }
      openSse({
        title: '重推',
        url: '/api/versionPushLog/add',
        data: {
          deviceId: [record.deviceId],
          versionId: props.versionId,
          versionPushTotalId: record.versionPushId,
        },
        method: 'post',
        onClose: async () => {
          tableRef.value.refresh();
        },
      });
    },
  });
};
</script>

<style lang="scss" scoped></style>
